package com.anysoft.guide;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.anysoft.R;
import com.anysoft.base.BaseActivity;
import com.anysoft.bean.GetStartupAdvertisementBean;
import com.anysoft.home.HomeActivity;
import com.anysoft.utils.MentorLogger;
import com.anysoft.utils.image.MyBitmapUtils;
import com.anysoft.view.CountDownView;
import com.anysoft.web.BannerUrlActivity;
import com.anysoft.web.FundDetailActivity;
import com.orhanobut.logger.Logger;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by wwq on 2016/12/6.
 */

public class GuideCountDownNewActivity extends BaseActivity {

    @BindView(R.id.iv_count_down_bg)
    ImageView ivCountDownBg;
    @BindView(R.id.count_down_view)
    CountDownView countDownView;
    @BindView(R.id.sdv_count_down_bg)
    SimpleDraweeView sdvCountDownBg;

    private CountDownView count_down_view;

    private GetStartupAdvertisementBean advertisementBean;

    private String imgUrl = "http://p19.qhimg.com/t01f797c7fe7a9f6448.png";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.guide_count_down_activity);
        ButterKnife.bind(this);

        advertisementBean = (GetStartupAdvertisementBean) getIntent().getSerializableExtra("bean");
        new MyBitmapUtils().disPlay(ivCountDownBg, advertisementBean.getData().getImage());

        Uri imageUri = Uri.parse(advertisementBean.getData().getImage());
        sdvCountDownBg.setImageURI(imageUri);

        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    private void initView() {

        count_down_view = (CountDownView) findViewById(R.id.count_down_view);
        count_down_view.setCountDownTimerListener(new CountDownView.CountDownTimerListener() {
            @Override
            public void onStartCount() {
                Logger.d(TAG, "开始计时");
            }

            @Override
            public void onFinishCount() {
                Logger.d(TAG, "结束计时");
                if (isGotoHome) {
                    startActivity(new Intent().setClass(GuideCountDownNewActivity.this, HomeActivity.class));
                    finish();
                }
            }
        });
        count_down_view.start();
    }

    private boolean isGotoHome = true;

    @OnClick({R.id.iv_count_down_bg, R.id.count_down_view, R.id.sdv_count_down_bg})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_count_down_bg:
            case R.id.sdv_count_down_bg:
//                showToast("点击广告图片~~~");
                if (advertisementBean != null && advertisementBean.getData() != null) {
                    String pathUrl = advertisementBean.getData().getUrl();
                    if (!TextUtils.isEmpty(pathUrl)) {
                        if (pathUrl.contains("fundDetail.html")) {
                            String url = null;
                            try {
                                url = URLDecoder.decode(pathUrl, "utf-8");
                            } catch (UnsupportedEncodingException e) {
                                e.printStackTrace();
                            }
                            MentorLogger.Log(TAG, "url=" + url);
                            String fundcode;
                            String fundType;
                            String mainTitle;
                            String subTitle;
                            Map<String, String> map = new HashMap<>();
                            String[] params = url.split("\\?")[1].split("&");
                            for (int i = 0; i < params.length; i++) {
                                //System.out.println("params--" + params[i]);
                                String key = params[i].split("=")[0];
                                String value = "";
                                try {
                                    value = params[i].split("=")[1];
                                } catch (Exception exception) {
                                    MentorLogger.Log(TAG, "value为null");
                                }
                                map.put(key, value);
                            }
                            fundcode = map.get("fundCode");
                            fundType = map.get("fundType");
                            mainTitle = map.get("fundName") + "  " + map.get("fundCode");
                            subTitle = map.get("fundstateDescription");

                            Intent mIntent = new Intent();
                            mIntent.setClass(mActivity, FundDetailActivity.class);
                            mIntent.putExtra("fundcode", fundcode);
                            mIntent.putExtra("fundType", fundType);
                            mIntent.putExtra("fundname", map.get("fundName"));
                            mIntent.putExtra("mainTitle", mainTitle);
                            mIntent.putExtra("subTitle", subTitle);
                            mIntent.putExtra("isFromGuide", true);
                            startActivity(mIntent);
                            isGotoHome = false;
                            count_down_view.stopTimer();
                            finish();
                        } else {
                            Intent intent = new Intent();
                            intent.setClass(mActivity, BannerUrlActivity.class);
                            intent.putExtra("bannerUrl", advertisementBean.getData().getUrl());
                            intent.putExtra("isFromGuide", true);
                            startActivity(intent);
                            isGotoHome = false;
                            count_down_view.stopTimer();
                            finish();
                        }
                    }
                }
                break;
            case R.id.count_down_view:
                startActivity(new Intent().setClass(GuideCountDownNewActivity.this, HomeActivity.class));
                count_down_view.stopTimer();
                finish();
                break;
        }
    }
}
